package his.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import his.entity.DiseCategory;
import his.persistence.DiseCategoryMapper;
import his.service.DiseCategoryService;
import org.example.his.common.api.CommonResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DiseCategoryImpl implements DiseCategoryService {

    @Autowired
    DiseCategoryMapper diseCategoryMapper;


    @Override
    public CommonResponse<List<DiseCategory>> getall_disecategory_bycodename(String keywords) {

        QueryWrapper<DiseCategory> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("DicaCode",keywords);
        List<DiseCategory> diseCategoryList1=diseCategoryMapper.selectList(queryWrapper1);
        if(diseCategoryList1.size()>0){
            return CommonResponse.createForSuccess(diseCategoryList1);
        }else{
            QueryWrapper<DiseCategory> queryWrapper2=new QueryWrapper<>();
            queryWrapper2.eq("DicaName",keywords);
            List<DiseCategory> diseCategoryList2=diseCategoryMapper.selectList(queryWrapper2);
            if(diseCategoryList2.size()>0){
                return CommonResponse.createForSuccess(diseCategoryList2);
            }else{
                return CommonResponse.createForError("搜索失败");
            }
        }

    }

    @Override
    public CommonResponse<List<DiseCategory>> delete_disecategory(int sequence_no) {

        QueryWrapper<DiseCategory> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("SequenceNo",sequence_no);
        List<DiseCategory> diseCategoryList=diseCategoryMapper.selectList(queryWrapper1);
        if(diseCategoryList.size()>0){
            QueryWrapper<DiseCategory> queryWrapper2=new QueryWrapper<>();
            queryWrapper2.eq("SequenceNo",sequence_no);
            int result=diseCategoryMapper.delete(queryWrapper2);
            return CommonResponse.createForSuccess(diseCategoryList);
        }else{
            return CommonResponse.createForError("删除失败");
        }

    }

    @Override
    public CommonResponse<List<DiseCategory>> update_disecategory(int sequence_no, String dicaCode, String dicaName, int sequenceNo, int dicaType) {

        QueryWrapper<DiseCategory> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("SequenceNo",sequence_no);
        List<DiseCategory> diseCategoryList=diseCategoryMapper.selectList(queryWrapper1);
        if(diseCategoryList.size()>0){
            QueryWrapper<DiseCategory> queryWrapper2=new QueryWrapper<>();
            queryWrapper2.eq("SequenceNo",sequence_no);
            DiseCategory diseCategory=new DiseCategory();
            diseCategory.setDicaCode(dicaCode);
            diseCategory.setDicaName(dicaName);
            diseCategory.setSequenceNo(sequenceNo);
            diseCategory.setDicaType(dicaType);
            diseCategory.setDelMark(1);
            int result=diseCategoryMapper.update(diseCategory,queryWrapper2);
            QueryWrapper<DiseCategory> queryWrapper3=new QueryWrapper<>();
            queryWrapper3.eq("SequenceNo",sequence_no);
            List<DiseCategory> diseCategoryList3=diseCategoryMapper.selectList(queryWrapper3);
            return CommonResponse.createForSuccess(diseCategoryList3);
        }else{
            return CommonResponse.createForError("修改失败");
        }

    }

    @Override
    public CommonResponse<List<DiseCategory>> add_disecategory(String dicaCode, String dicaName, int sequenceNo, int dicaType) {

        QueryWrapper<DiseCategory> queryWrapper1=new QueryWrapper<>();
        queryWrapper1.eq("DicaName",dicaName);
        List<DiseCategory> diseCategoryList1=diseCategoryMapper.selectList(queryWrapper1);
        if(diseCategoryList1.size()>0){
            return CommonResponse.createForError("新增失败，名称已存在");
        }else{
            QueryWrapper<DiseCategory> queryWrapper2=new QueryWrapper<>();
            queryWrapper2.eq("SequenceNo",sequenceNo);
            List<DiseCategory> diseCategoryList2=diseCategoryMapper.selectList(queryWrapper2);
            if(diseCategoryList2.size()>0){
                return CommonResponse.createForError("新增失败，编号已存在");
            }else{
                DiseCategory diseCategory=new DiseCategory();
                diseCategory.setDicaCode(dicaCode);
                diseCategory.setDicaName(dicaName);
                diseCategory.setSequenceNo(sequenceNo);
                diseCategory.setDicaType(dicaType);
                diseCategory.setDelMark(1);
                diseCategoryMapper.insert(diseCategory);
                QueryWrapper<DiseCategory> queryWrapper3=new QueryWrapper<>();
                queryWrapper3.eq("SequenceNo",sequenceNo);
                List<DiseCategory> diseCategoryList3=diseCategoryMapper.selectList(queryWrapper3);
                return CommonResponse.createForSuccess(diseCategoryList3);
            }
        }

    }

    @Override
    public CommonResponse<List<DiseCategory>> getall_disecategorys() {

        QueryWrapper<DiseCategory> queryWrapper=new QueryWrapper<>();
        List<DiseCategory> diseCategoryList=diseCategoryMapper.selectList(queryWrapper);
        if(diseCategoryList.size()>0){
            return CommonResponse.createForSuccess(diseCategoryList);
        }else{
            return CommonResponse.createForError("查询失败");
        }

    }
}
